package cn.csust.net2.manager.client.panel;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.ModifyPasswordService;
import cn.csust.net2.manager.shared.service.ModifyPasswordServiceAsync;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ModifyPasswordPanel extends ContentPanel {
	
	private TextField<String> passwordOld;
	private TextField<String> passwordNew;
	private TextField<String> passwordConfirm;
	
	public ModifyPasswordPanel() {
		setHeaderVisible(false);
		setLayout(new CenterLayout());
		create();
	}
	
	public void create() {
		FormData formData = new FormData("-20");
		final FormPanel formPanel = new FormPanel();
		formPanel.setHeading("请输入你要修改的密码...");
		formPanel.setFrame(true);
		formPanel.setLabelWidth(80);
		formPanel.setWidth(500);
		
		passwordOld = new TextField<String>();
		passwordOld.setPassword(true);
		passwordOld.setFieldLabel("旧密码");
		passwordOld.setAllowBlank(false);
		formPanel.add(passwordOld, formData);
		
		passwordNew = new TextField<String>();
		passwordNew.setPassword(true);
		passwordNew.setFieldLabel("新密码");
		passwordNew.setAllowBlank(false);
		formPanel.add(passwordNew, formData);
		
		passwordConfirm = new TextField<String>();
		passwordConfirm.setPassword(true);
		passwordConfirm.setFieldLabel("确认新密码");
		passwordConfirm.setAllowBlank(false);
		formPanel.add(passwordConfirm, formData);
		
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);  
		
		Button saveButton = new Button("提交");
		formPanel.addButton(saveButton);
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				submit();
			}
		});
		
		Button resetButton = new Button("重置");
		formPanel.addButton(resetButton);
		resetButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				formPanel.reset();
			}
		});
		
		this.add(formPanel);
	}
	
	public void submit() {
		User user = App.getUser();
		String oldPassword = user.getPassword();
		//前台验证
		if(!oldPassword.equals(passwordOld.getRawValue())) {
			MessageBox.alert("提示", "对不起，您输入的旧密码有误，请重新输入！", null);
			return ;
		}
		
		if(!passwordNew.getRawValue().equals(passwordConfirm.getRawValue())) {
			MessageBox.alert("提示", "对不起，您前后两次输入的密码不相同，请重新输入！", null);
			return ;
		}
		
		ModifyPasswordServiceAsync service = GWT.create(ModifyPasswordService.class);
		service.modifyPassword(user, passwordNew.getRawValue(), new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Info.display("提示", "密码修改成功！！");
				App.getUser().setPassword(passwordNew.getRawValue());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				MessageBox.alert("提示", "对不起，由于系统出现错误，密码修改失败，请稍后重试或与管理员联系。", null);
			}
		});
	}
}
